//
//  RootViewController.m
//  SeseNews
//
//  Created by Sese on 15/7/22.
//  Copyright (c) 2015年 Sese工作室. All rights reserved.
//

#import "RootViewController.h"
#import "Arrow.h"
#import "ListBarSV.h"
#import "DeleteBar.h"
#import "DetailsListSV.h"
#import "ListItemBar.h"
#import "basedTVC.h"



#define kListBarH 30
#define kArrowW 40
#define kAnimationTime 0.8

@interface RootViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)ListBarSV *listBarSV;
@property(nonatomic,strong)DeleteBar *deleteBar;
@property(nonatomic,strong)DetailsListSV *detailListSV;
@property(nonatomic,strong)Arrow *arrow;
@property(nonatomic,strong)UIScrollView *mainScroller;

@property(nonatomic,strong)NSMutableArray *topArray;
@property(nonatomic,strong)NSMutableArray *tabVCArray;
@property(nonatomic,strong)NSMutableDictionary *tabVCDic;

@property(nonatomic,strong) NSArray *arrayLists;

@end

@implementation RootViewController

- (NSArray *)arrayLists
{
    if (_arrayLists == nil) {
        _arrayLists = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"NewsURLs.plist" ofType:nil]];
    }
    return _arrayLists;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    basedTVC *vc1 = [[basedTVC alloc]init];
    vc1.urlString = self.arrayLists[0][@"urlString"];
    [self addChildViewController:vc1];
    basedTVC *vc2 = [[basedTVC alloc]init];
    vc2.urlString = self.arrayLists[1][@"urlString"];
    [self addChildViewController:vc2];
    basedTVC *vc3 = [[basedTVC alloc]init];
    vc3.urlString = self.arrayLists[2][@"urlString"];
    [self addChildViewController:vc3];
    basedTVC *vc4 = [[basedTVC alloc]init];
    vc4.urlString = self.arrayLists[3][@"urlString"];
    [self addChildViewController:vc4];
    basedTVC *vc5 = [[basedTVC alloc]init];
    vc5.urlString = self.arrayLists[4][@"urlString"];
    [self addChildViewController:vc5];
    basedTVC *vc6 = [[basedTVC alloc]init];
    vc6.urlString = self.arrayLists[5][@"urlString"];
    [self addChildViewController:vc6];
    basedTVC *vc7 = [[basedTVC alloc]init];
    vc7.urlString = self.arrayLists[6][@"urlString"];
    [self addChildViewController:vc7];
    basedTVC *vc8 = [[basedTVC alloc]init];
    vc8.urlString = self.arrayLists[7][@"urlString"];
    [self addChildViewController:vc8];
    basedTVC *vc9 = [[basedTVC alloc]init];
    vc9.urlString = self.arrayLists[8][@"urlString"];
    [self addChildViewController:vc9];
    basedTVC *vc10 = [[basedTVC alloc]init];
    vc10.urlString = self.arrayLists[9][@"urlString"];
    [self addChildViewController:vc10];
    basedTVC *vc11 = [[basedTVC alloc]init];
    vc11.urlString = self.arrayLists[10][@"urlString"];
    [self addChildViewController:vc11];
    basedTVC *vc12 = [[basedTVC alloc]init];
    vc12.urlString = self.arrayLists[11][@"urlString"];
    [self addChildViewController:vc12];
    basedTVC *vc13 = [[basedTVC alloc]init];
    vc13.urlString = self.arrayLists[12][@"urlString"];
    [self addChildViewController:vc13];
    basedTVC *vc14 = [[basedTVC alloc]init];
    vc14.urlString = self.arrayLists[13][@"urlString"];
    [self addChildViewController:vc14];
    basedTVC *vc15 = [[basedTVC alloc]init];
    vc15.urlString = self.arrayLists[14][@"urlString"];
    [self addChildViewController:vc15];
    basedTVC *vc16 = [[basedTVC alloc]init];
    vc16.urlString = self.arrayLists[15][@"urlString"];
    [self addChildViewController:vc16];
    basedTVC *vc17 = [[basedTVC alloc]init];
    vc17.urlString = self.arrayLists[16][@"urlString"];
    [self addChildViewController:vc14];
    basedTVC *vc18 = [[basedTVC alloc]init];
    vc18.urlString = self.arrayLists[17][@"urlString"];
    [self addChildViewController:vc15];
    basedTVC *vc19 = [[basedTVC alloc]init];
    vc19.urlString = self.arrayLists[18][@"urlString"];
    [self addChildViewController:vc16];
    
    
    self.tabVCDic = [NSMutableDictionary dictionaryWithObjects:@[vc1,vc2,vc3,vc4,vc5,vc6,vc7,vc8,vc9,vc10,vc11,vc12,vc13,vc14,vc15,vc16,vc17,vc18,vc19] forKeys:@[@"头条",@"财经",@"FUN",@"游戏",@"数码",@"娱乐",@"跑步",@"城市",@"星座",@"自行车",@"文化",@"旅游",@"读书",@"体育",@"房产",@"科技",@"健康",@"评论",@"佛教"]];
    
    self.TabVCArray = [NSMutableArray arrayWithObjects:vc1,vc2,vc3,vc4,vc5,vc6,vc7,vc8,vc9, nil];
   
    
    [self setupNaviBar];
    [self makeContent];
}

-(void)setupNaviBar
{    [self.navigationController.navigationBar setBackgroundColor:[UIColor cyanColor]];
    [self.navigationController.navigationBar setTranslucent:NO];
    //修改状态栏颜色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav1.jpg"] forBarMetrics:UIBarMetricsDefault];
    
    //设置左边的Logo
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"SeseNews" style:UIBarButtonItemStylePlain target:nil action:nil];
    leftBarButtonItem.enabled = NO;
    UIFont *front = [UIFont fontWithName:@"Snell Roundhand"size:32.0];
    NSDictionary *textAttributes = @{NSFontAttributeName:front,NSForegroundColorAttributeName:[UIColor colorWithWhite:0.400 alpha:1.000]};
    [leftBarButtonItem setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    UIBarButtonItem *rightBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"•••" style:UIBarButtonItemStylePlain target:nil action:nil];
    UIFont *rightfront = [UIFont fontWithName:@"Heiti SC"size:18.0];
    NSDictionary *textRAttributes = @{NSFontAttributeName:rightfront,NSForegroundColorAttributeName:[UIColor colorWithWhite:0.600 alpha:1.000]};
    [rightBarButtonItem setTitleTextAttributes:textRAttributes forState:UIControlStateNormal];    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    
}

//实现更多按钮方法
-(void)evenMore{
    
}

-(void)makeContent
{
    NSMutableArray *listTop = [[NSMutableArray alloc] initWithArray:@[@"头条",@"财经",@"FUN",@"游戏",@"数码",@"娱乐",@"跑步",@"城市",@"星座"]];
    NSMutableArray *listBottom = [[NSMutableArray alloc] initWithArray:@[@"自行车",@"文化",@"旅游",@"读书",@"体育",@"房产",@"科技",@"健康",@"评论",@"佛教"]];
    
    //self进行weak化，否则造成循环引用无法释放controller
    __weak typeof(self) unself = self;
    
    if (!self.detailListSV) {
        self.detailListSV = [[DetailsListSV alloc] initWithFrame:CGRectMake(0, kListBarH-kScreenH, kScreenW, kScreenH-kListBarH)];
        self.detailListSV.listAll = [NSMutableArray arrayWithObjects:listTop,listBottom, nil];
        self.detailListSV.longPressedBlock = ^(){
            [unself.deleteBar sortBtnClick:unself.deleteBar.sortBtn];
        };
        self.detailListSV.opertionFromItemBlock = ^(animateType type, NSString *itemName, int index){
            [unself.listBarSV operationFromBlock:type itemName:itemName index:index];
        };
        [self.view addSubview:self.detailListSV];
    }
    
    if (!self.listBarSV) {
        self.listBarSV = [[ListBarSV alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kListBarH)];
        self.listBarSV.visibleItemList = listTop;
        self.listBarSV.arrowChange = ^(){
            if (unself.arrow.arrowBtnClick) {
                unself.arrow.arrowBtnClick();
            }
        };
        unself.listBarSV.dataArray = ^(NSMutableArray *dataArray){
            self.topArray = dataArray;
        };
        //对activity对象添加观察者，观察他的coveImage属性是否有变化；现在观察的是是否有新图片改变；
        [self addObserver:self forKeyPath:@"topArray" options:NSKeyValueObservingOptionNew context:nil];
        
        
        //相应点击事件
        self.listBarSV.listBarItemClickBlock = ^(NSString *itemName , NSInteger itemIndex){
            [unself.detailListSV itemRespondFromListBarClickWithItemName:itemName];
            //添加scrollview
            
            //将视图移动到该位置
            unself.mainScroller.contentOffset =  CGPointMake(itemIndex * unself.mainScroller.frame.size.width, 0);
            
            
        };
        
        [self.view addSubview:self.listBarSV];
    }
    
    if (!self.deleteBar) {
        self.deleteBar = [[DeleteBar alloc] initWithFrame:self.listBarSV.frame];
        [self.view addSubview:self.deleteBar];
    }
    
    
    if (!self.arrow) {
        self.arrow = [[Arrow alloc] initWithFrame:CGRectMake(kScreenW-kArrowW, 0, kArrowW, kListBarH)];
        self.arrow.arrowBtnClick = ^(){
            unself.deleteBar.hidden = !unself.deleteBar.hidden;
            [UIView animateWithDuration:kAnimationTime animations:^{
                CGAffineTransform rotation = unself.arrow.imageView.transform;
                unself.arrow.imageView.transform = CGAffineTransformRotate(rotation,M_PI);
                unself.detailListSV.transform = (unself.detailListSV.frame.origin.y<0)?CGAffineTransformMakeTranslation(0, kScreenH):CGAffineTransformMakeTranslation(0, -kScreenH);
            }];
        };
        [self.view addSubview:self.arrow];
    }
    
    if (!self.mainScroller) {
        self.mainScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kListBarH, kScreenW , kScreenH-kListBarH-64)];
        self.mainScroller.backgroundColor = [UIColor blackColor];
        self.mainScroller.bounces = YES;
        self.mainScroller.pagingEnabled = YES;
        self.mainScroller.showsHorizontalScrollIndicator = NO;
        self.mainScroller.showsVerticalScrollIndicator = NO;
        self.mainScroller.delegate = self;
        self.mainScroller.contentSize = CGSizeMake(kScreenW*_tabVCArray.count,self.mainScroller.frame.size.height);
        //是否自动裁切超出部分
        self.mainScroller.clipsToBounds = YES;
        [self.view insertSubview:self.mainScroller atIndex:0];
        
        for (int i=0 ; i<9; i++) {
            [self addScrollViewWithItemName:[NSString stringWithFormat:@"%@",self.listBarSV.visibleItemList[i]] index:i];
        }
        
        
        
    }
}

//重写观察方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    self.mainScroller.contentSize = CGSizeMake(kScreenW*_topArray.count,self.mainScroller.frame.size.height);
    
    [self.tabVCArray removeAllObjects];
    basedTVC *tab = [[basedTVC alloc]init];
    
    for (int i=0; i<_topArray.count; i++) {
        
        NSString *keyStr=_topArray[i];
        tab=[self.tabVCDic objectForKey:keyStr];
        
        [self.tabVCArray addObject:tab];
    }
    
    
    for (int i=0 ; i<self.tabVCArray.count; i++) {
        [self addScrollViewWithItemName:[NSString stringWithFormat:@"%@",self.listBarSV.visibleItemList[i]] index:i];
    }
    
    
    
}


-(void)addScrollViewWithItemName:(NSString *)itemName index:(NSInteger)index{

    UITableViewController * tableC = _tabVCArray[index];
    UITableView * tableV = tableC.tableView;
    tableV.frame = CGRectMake(index * self.mainScroller.frame.size.width, 0, self.mainScroller.frame.size.width, self.mainScroller.frame.size.height);
    [self addChildViewController:tableC];
    [self.mainScroller addSubview:tableV];
    
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    [self.listBarSV itemClickByScrollerWithIndex:scrollView.contentOffset.x / self.mainScroller.frame.size.width];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
